package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.CommentAdapter;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.CommentBean;
import tq.com.tqstudent.bean.CommentScoreBean;
import tq.com.tqstudent.bean.ScoreBean;
import tq.com.tqstudent.bean.event.CreateCommentEvent;
import tq.com.tqstudent.net.ok.DialogCallback;
import tq.com.tqstudent.views.CommentHeader;

/**
 * @Author rames
 * @date 2019/2/28 17:57
 */
public class CommentActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
//    private CommonAdapter adapter;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    private int pageNO = 1;
    private CommentHeader header;
    private CommentAdapter commentAdapter;

    public static void show(Activity activity){
        Intent intent = new Intent(activity, CommentActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    public int initLayout() {
        return R.layout.activity_comment;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("评价");

        header = new CommentHeader(this);
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        commentAdapter = new CommentAdapter(this);
        commentAdapter.addHeaderView(header);
        recyclerView.setAdapter(commentAdapter);

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNO++;
                getData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNO = 1;
                getData();
            }
        });
    }

    @Subscribe
    public void OnCreateCommentEvent(CreateCommentEvent event){
        refreshLayout.autoRefresh();
    }

    @OnClick(R.id.build_comment_tv)
    public void onClick(View view){
        CreateCommentActivity.show(this, "1");
    }

    @Override
    public void initData() {
        getData();
    }

    private void initHeadView(CommentScoreBean data){
        ArrayList<Integer> scores = new ArrayList<>();
        double peopleCount = 0; // 总人数
        double scoreCount = 0; // 总分数
        for (int i = 0; i < data.score.size(); i++) {
            ScoreBean score = data.score.get(i);
            peopleCount += score.count;
            scoreCount += score.count*(score.score*20);
            scores.add(score.percent);
        }

//        LogUtil.e("总人数:"+peopleCount+" | 总分数: "+scoreCount);
        header.setValue(scores, String.valueOf(data.total_cnt), (int)(scoreCount/data.total_cnt));
    }

    private void getData(){
        String uri = "api/clz/score/";
        OkGo.<BaseBean<CommentScoreBean>>get(App.getHttpUrl(uri)+ Constants.PAGE_SIZE+"/"+pageNO)
                .tag(this)
                .execute(new DialogCallback<CommentScoreBean>(this){
                    @Override
                    public void handleSuccess(CommentScoreBean data) {
                        // 这是加载更多
                        if(refreshLayout.getState() == RefreshState.Loading){
                            commentAdapter.addData(data.comment);
                            if(data.comment.size() < Constants.PAGE_SIZE){
                                refreshLayout.setEnableLoadMore(false);
                            }

                            // 这是下拉刷新
                        } else {
                            initHeadView(data);
                            refreshLayout.setEnableLoadMore(true);
                            commentAdapter.setData(data.comment);
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                        emptyLayout.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
                    }
                });
    }
}
